package com.shoeshopping.service.pc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.shoeshopping.dao.pc.GetIntoCartDao;
import com.shoeshopping.entity.GoodsItem;
import com.shoeshopping.entity.OrderItem;
import com.shoeshopping.entity.ShoppingCart;
import com.shoeshopping.entity.User;
import com.shoeshopping.service.joseph.GoodsService;
import com.shoeshopping.utils.pc.Result;

@Transactional
@Service("getIntoCartService")
public class GetIntoCartServiceImpl implements GetIntoCartService {
	
	@Resource(name="getIntoCartDao")
	GetIntoCartDao getIntoCartDao;
	
	@Resource(name="goodsService")
	GoodsService goodsService;
	
	public GetIntoCartDao getGetIntoCartDao() {
		return getIntoCartDao;
	}

	public void setGetIntoCartDao(GetIntoCartDao getIntoCartDao) {
		this.getIntoCartDao = getIntoCartDao;
	}
	
	@Override
	public String showCart() {

		Map Session = ActionContext.getContext().getSession();
		User u = (User) Session.get("user");
		ShoppingCart sc = u.getShoppingCart();
		
		if(sc == null) {
			sc = new ShoppingCart();
			List<GoodsItem> giList = new ArrayList<GoodsItem>();
			sc.setUser(u);
			sc.setGoodsItems(giList);
			getIntoCartDao.save(sc);
		} else {
			String hql = "from GoodsItem where shoppingCart.id = ?";
			List param = new ArrayList();
			param.add(u.getShoppingCart().getId());
			List list = getIntoCartDao.find(hql, param);
			sc.setGoodsItems(list);
		}
		
		u.setShoppingCart(sc);
		Session.remove("user");
		Session.put("user", u);
		List Glist = u.getShoppingCart().getGoodsItems();
		List list = (List) Session.get("goodsItemList");
		if(list != null && list.size() > 0) {
			Session.remove("goodsItemList");
		}
		Session.put("goodsItemList", Glist);
		
		return Result.SUCCESS;
	}

	@Override
	public String save(GoodsItem goodsItem) {
		
		Map Session = ActionContext.getContext().getSession();
		User u = (User) Session.get("user");
		String userid = u.getId();
		
		String hql2 = "from ShoppingCart where user.id = ?";
		List param2 = new ArrayList();
		param2.add(userid);
		ShoppingCart sc = (ShoppingCart) getIntoCartDao.find(hql2, param2).get(0);
		
		GoodsItem gi = new GoodsItem();
		
		int num = goodsItem.getNum();
		
		String goodsId = goodsItem.getGoods().getId();
		gi.setGoods(goodsService.displayGoodsById(goodsId));
		gi.setNum(goodsItem.getNum());
		
		
		if(sc == null) {                                 				 //无购物车
			System.out.println("+++++++++++++++++无购物车");
			sc = new ShoppingCart();
			List<GoodsItem> giList = new ArrayList<GoodsItem>();
			gi.setShoppingCart(sc);
			giList.add(gi);
			sc.setGoodsItems(giList);
			sc.setUser(u);
			getIntoCartDao.save(sc);
			//System.out.println("-------------save shoppingcart");
		} else {                                      					 //有购物车
			
			String hql = "from GoodsItem gi where gi.goods.id = ?";
			List param = new ArrayList();
			param.add(goodsId);
			List resultForGoodsItem = getIntoCartDao.find(hql,param);
			
			if(resultForGoodsItem.size() > 0) {             			  //有重复商品
				
				System.out.println("+++++++++++++++++++++++有重复商品");
				
				GoodsItem temp = (GoodsItem) resultForGoodsItem.get(0);
				if(num+temp.getNum()>temp.getGoods().getRest()) {             //库存不足
					return Result.ERROR_QUANTITY;
				} else {																//库存够
					
					System.out.println("++++++++++++++商品存在且数量不超过容量");
					//resultForGoodsItem.remove(temp);
					temp.setNum(goodsItem.getNum()+temp.getNum());
					//resultForGoodsItem.add(temp);
					//sc.setGoodsItems(resultForGoodsItem);
					//getIntoCartDao.update(sc);
					getIntoCartDao.update(temp);
				}
			} else {                                               		   //无重复商品
				
				System.out.println("+++++++++++++++++++++++++无重复商品");
				
				gi.setShoppingCart(sc);
				List<GoodsItem> giList = sc.getGoodsItems();
				giList.add(gi);
				sc.setGoodsItems(giList);
				getIntoCartDao.save(gi);
			}
		}
		
		u.setShoppingCart(sc);
		Session.remove("user");
		Session.put("user", u);
		Session.remove("goodsItemList");
		Session.put("goodsItemList", sc.getGoodsItems());
		
		
		return Result.SUCCESS;
	}

	@Override
	public String update(GoodsItem goodsItem) {
		if(goodsItem.getGoods().getRest() <  goodsItem.getNum()) {
			return Result.ERROR_QUANTITY;
		}
		getIntoCartDao.update(goodsItem);
		return Result.SUCCESS;
	}

	@Override
	public String delete(GoodsItem goodsItem) {
		
		String hql = "from ShoppingCart where id=?";
		List param = new ArrayList();
		param.add(goodsItem.getShoppingCart().getId());
		List scList = getIntoCartDao.find(hql, param);
		ShoppingCart sc = (ShoppingCart) scList.get(0);
		
		String hql1 = "from GoodsItem where id=?";
		List param1 = new ArrayList();
		param1.add(goodsItem.getId());
		List giList = getIntoCartDao.find(hql1, param1);
		GoodsItem gi = (GoodsItem) giList.get(0);
		
		List list = sc.getGoodsItems();
		System.out.println("-----size = " + list.size());
		
		list.remove(gi);
		//sc.setGoodsItems(list);
		
		System.out.println("-----size1 = " + list.size());
		
		getIntoCartDao.delete(gi);
		getIntoCartDao.update(sc);
		
		Map Session = ActionContext.getContext().getSession();
		
		List GList = (List) Session.get("goodsItemList");
		if(GList != null && GList.size() > 0) {
			Session.remove("goodsItemList");
		}
		Session.put("goodsItemList", list);
		
		return Result.SUCCESS;
	}
	
	

	@Override
	public String addToOrder(GoodsItem goodsItem) {

		String hql = "from ShoppingCart where id=?";
		List param = new ArrayList();
		param.add(goodsItem.getShoppingCart().getId());
		List scList = getIntoCartDao.find(hql, param);
		ShoppingCart sc = (ShoppingCart) scList.get(0);
		
		if(sc.getGoodsItems()==null) {
			return Result.ERROR;
		}
		return Result.SUCCESS;
	}

	
	
	@Override
	public String deleteByCartId(String cartId) {

		getIntoCartDao.deleteByCartId(cartId);
		
		return null;
	}

	@Override
	//@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List find(String shoppingCartId) {
		String hql = "from GoodsItem gi where gi.shoppingCart.id = '" + shoppingCartId + "'";
		List list = getIntoCartDao.find(hql);
		return list;
	}

	
	
}
